package com.company.project.service.impl;

import com.company.project.dao.NewsDetailMapper;
import com.company.project.dao.NewsMapper;
import com.company.project.model.News;
import com.company.project.model.Periodicals;
import com.company.project.service.NewsService;
import com.company.project.core.AbstractService;
import com.company.project.util.PageUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * Created by CodeGenerator on 2018/10/08.
 */
@Service
@Transactional
public class NewsServiceImpl extends AbstractService<News> implements NewsService {
    @Resource
    private NewsMapper newsMapper;
    @Resource
    private NewsDetailMapper newsDetailMapper;
    @Value("${file.imguploadFolder}")
    private String path;

    //根据类型获取新闻
    @Override
    public Map<String, List<News>> getNewsByDeployModule() {
        List<News> newsList = newsMapper.selectNewsByDeployModule();
        Map<String, List<News>> rMap = new HashMap<>();
        for (int i = 0; i < newsList.size(); i++) {
            if (newsList.get(i).getDeployModule() != null) {
                rMap.put(newsList.get(i).getDeployModule(), null);
            }
        }
        for (Map.Entry<String, List<News>> entry : rMap.entrySet()) {
            List<News> news = new ArrayList();
            for (int k = 0; k < newsList.size(); k++) {
                if (entry.getKey().equals(newsList.get(k).getDeployModule())) {
                    if (newsList.get(k).getLastModifyTime() != null) {
                        newsList.get(k).setCreateTimeFormate(newsList.get(k).getCreateTimeFormate().substring(5));
                    }
                    news.add(newsList.get(k));
                }
            }
            entry.setValue(news);
        }

        return rMap;
    }

    @Override
    public PageUtil<News> getNewsAllByDeployModule(String deployModule, Integer pageIndex) {
        PageUtil<News> pageUtil = new PageUtil();
        pageUtil.setPageSize(10);
        if (("articleMess").equals(deployModule) || ("activity").equals(deployModule)) {
            pageUtil.setPageSize(5);
        }
        pageUtil.setCpage(pageIndex);
        pageUtil.setTotalRow(this.getAllCount(deployModule));
        pageUtil.setLists(newsMapper.selectAllNewsByDeployModule(deployModule, pageUtil));
        return pageUtil;
    }

    @Override
    public Integer getAllCount(String deployModule) {
        return newsMapper.selectAllCount(deployModule);
    }

    @Transactional
    @Override
    public List selectNews(String title) {
        return newsMapper.selectNews(title);
    }

    @Override
    public News getNewsById(Integer id) {
        return newsMapper.selectNewsById(id);
    }

    /**
     * 添加工作动态
     *
     * @param news
     * @return
     */
    @Override
    public Boolean addWorkTrend(News news) {
        if (news.getDeployModule().equals("inform")) {
            news.setUri("text03");
        } else if (news.getDeployModule().equals("dynamic")) {
            news.setUri("text02");
        } else if (news.getDeployModule().equals("worktrend")) {
            news.setUri("text01");
        } else if (news.getDeployModule().equals("activity")){
            news.setUri("textCompile");
        } else if (news.getDeployModule().equals("articleMess")){
            news.setUri("textCompile");
        }else if (news.getDeployModule().equals("league")){
            news.setUri("textLeague");
        }else if (news.getDeployModule().equals("policy")){
            news.setUri("textCompile");
        }else if (news.getDeployModule().equals("special")) {
            news.setUri("textCompile");
        }
        Boolean aBoolean = newsMapper.addWorkTrend(news);
        Boolean aBoolean1 = newsDetailMapper.addMessage(news);
        if (aBoolean.equals(true) && aBoolean1.equals(true)) {
            return true;
        }
        return false;
    }

    /**
     * 删除工作动态
     *
     * @param news
     * @return
     */
    @Override
    public Boolean delWorkTrend(News news) {
        News news1 = newsMapper.selectUrl(news.getId());
        if (news1.getPhoneUri()!=""){
            String phoneUri = news1.getPhoneUri();
            int beginIndex = phoneUri.lastIndexOf("/");
            int endIndex = phoneUri.length();
            String substring = phoneUri.substring(beginIndex + 1, endIndex);
            String fullFilePath = path+substring;
            File delfile = new File(fullFilePath);
            Boolean aBoolean = newsDetailMapper.deleteNewDetail(news);
            Boolean aBoolean1 = newsMapper.delWorkTrend(news);
            if (aBoolean.equals(true) && aBoolean1.equals(true)) {
                if (delfile.exists() && delfile.isFile()
                        && delfile.delete() == true) {
                    return true;
                }
                return false;
            }
        }else {
            Boolean aBoolean = newsDetailMapper.deleteNewDetail(news);
            Boolean aBoolean1 = newsMapper.delWorkTrend(news);
            if (aBoolean.equals(true) && aBoolean1.equals(true)) {
                return true;
            }
        }
        return false;
    }


    @Override
    public PageUtil<News> selectNewsAll(String deployModule, Integer pageIndex) {
        PageUtil<News> pageUtil = new PageUtil();
        pageUtil.setPageSize(15);
        pageUtil.setCpage(pageIndex);
        pageUtil.setTotalRow(this.selectNewsCount(deployModule));
        pageUtil.setLists(newsMapper.selectNewsAll(deployModule, pageUtil));
        return pageUtil;
    }

    @Override
    public Integer selectNewsCount(String deployModule) {
        return newsMapper.selectNewsCount(deployModule);
    }

    /**
     * 修改工作动态
     */
    @Override
    public Boolean updateWorkTrend(News news) {
        if (news.getDeployModule().equals("inform")) {
            news.setUri("text03");
        } else if (news.getDeployModule().equals("dynamic")) {
            news.setUri("text02");
        } else if (news.getDeployModule().equals("worktrend")) {
            news.setUri("text01");
        } else if (news.getDeployModule().equals("activity")){
            news.setUri("textCompile");
        } else if (news.getDeployModule().equals("articleMess")){
            news.setUri("textCompile");
        }else if (news.getDeployModule().equals("league")){
            news.setUri("textLeague");
        }else if (news.getDeployModule().equals("policy")){
            news.setUri("textCompile");
        }else if (news.getDeployModule().equals("special")) {
            news.setUri("textCompile");
        }
        Boolean aBoolean = newsMapper.updateWorkTrend(news);
        Boolean aBoolean1 = newsDetailMapper.upNewsD(news);
        if (aBoolean.equals(true) && aBoolean1.equals(true)) {
            return true;
        }
        return false;
    }

    /**
     * 直接上传
     *
     * @param news
     * @return
     */
    @Override
    public Boolean updateNews(News news) {
        if (news.getDeployModule().equals("inform")) {
            news.setUri("text03");
        } else if (news.getDeployModule().equals("dynamic")) {
            news.setUri("text02");
        } else if (news.getDeployModule().equals("worktrend")) {
            news.setUri("text01");
        } else if (news.getDeployModule().equals("activity")){
            news.setUri("textCompile");
        } else if (news.getDeployModule().equals("articleMess")){
            news.setUri("textCompile");
        }else if (news.getDeployModule().equals("league")){
            news.setUri("textLeague");
        }else if (news.getDeployModule().equals("policy")){
            news.setUri("textCompile");
        }else if (news.getDeployModule().equals("special")) {
            news.setUri("textCompile");
        }
        Boolean aBoolean = newsMapper.updateNews(news);
        Boolean aBoolean1 = newsDetailMapper.upNewsD(news);
        if (aBoolean.equals(true) && aBoolean1.equals(true)) {
            return true;
        }
        return false;
    }

    /**
     * 添加上架
     *
     * @param news
     * @return
     */
    @Override
    public Boolean addPutAway(News news) {
        if (news.getDeployModule().equals("inform")) {
            news.setUri("text03");
        } else if (news.getDeployModule().equals("dynamic")) {
            news.setUri("text02");
        } else if (news.getDeployModule().equals("worktrend")) {
            news.setUri("text01");
        } else if (news.getDeployModule().equals("activity")){
            news.setUri("textCompile");
        } else if (news.getDeployModule().equals("articleMess")){
            news.setUri("textCompile");
        }else if (news.getDeployModule().equals("league")){
            news.setUri("textLeague");
        }else if (news.getDeployModule().equals("policy")){
            news.setUri("textCompile");
        }else if (news.getDeployModule().equals("special")) {
            news.setUri("textCompile");
        }
        Boolean aBoolean = newsMapper.addPutAway(news);
        Boolean aBoolean1 = newsDetailMapper.addMessage(news);
        if (aBoolean.equals(true) && aBoolean1.equals(true)) {
            return true;
        }
        return false;
    }

    /**
     * 前台搜索框搜索活动
     * @param cname
     * @param index
     * @return
     */
    @Override
    public PageUtil<Map<Object,Object>> queryNews(String cname, Integer index) {
        PageUtil<Map<Object,Object>> pageUtil = new PageUtil();
        pageUtil.setPageSize(15);
        pageUtil.setCpage(index);
        pageUtil.setTotalRow(this.queryNewsCount(cname));
        List<Map<Object, Object>> maps = newsMapper.queryNews(pageUtil, cname);
        for (int i = 0;i<maps.size(); i++){
            maps.get(i).put("value","活动");
        }
        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
        for (Map<Object,Object> map : maps){
            map.put("create_time",format.format(map.get("create_time")));
        }
        pageUtil.setLists(maps);
        return pageUtil;
    }

    @Override
    public Integer queryNewsCount(String cname) {
        return newsMapper.queryNewsCount(cname);
    }
}
